                                     
package com.aol.library;

import java.util.List;
import java.util.Properties;
import static junit.framework.Assert.*;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import com.selenium.library.Driver;

/* Author - Payal Garg
TestName - Main AOL Library file
Date created - 12-06-2013
*/

public class AolLibrary extends Driver{

 	static WebDriver wd;
 	String bro;
 	Driver driver;
  	static Properties props = new Properties();
  	static Properties properties = new Properties();

	public AolLibrary() throws Exception{
		
//			properties.load(Driver.class.getClassLoader().getResourceAsStream("resources/config.properties"));		
	 		bro = System.getProperty("Browser");
	 		driver = new Driver();
			int browe = 0;
			if (bro.equalsIgnoreCase("firefox")){
				 browe = 1;
			}
			 if (bro.equalsIgnoreCase("internetexplorer")){
				browe = 2;
			 }
			if (bro.equalsIgnoreCase("safari")){
				 browe = 3;
			}
			if (bro.equalsIgnoreCase("chrome")){
				 browe = 4;
			}
			switch(browe){
			 case 1: 
				 props.load(AolLibrary.class.getClassLoader().getResourceAsStream("resources/FFGUI.properties"));
				break;
			 case 2:
	 			 props.load(AolLibrary.class.getClassLoader().getResourceAsStream("resources/IEGUI.properties"));
				break;
			 case 3:
	 			 props.load(AolLibrary.class.getClassLoader().getResourceAsStream("resources/SafariGUI.properties"));
				break;
			 case 4:
	 			 props.load(AolLibrary.class.getClassLoader().getResourceAsStream("resources/chromeGUI.properties"));
				break;
			 default:
				throw new RuntimeException("Doesn't support the selected Browser for Automation");
			}
	}
 
	
		
	/* Author - Payal Garg
	Function - It maximizes the browser window
	Date created - 29-04-2013*/
	public void maximise()throws Exception{
 		 driver.maximise();
 	 }
	
	
	
	
	
	/* Author - Payal Garg
	Function - This function opens URL in desired webdriver
	Arguments - URL
	Date created - 29-04-2013 */
	public void openURL(String URL) throws Exception{
 		driver.open(URL, bro);
		}
	
	
	
	
	/* Author - Payal Garg
	Function - This function Sync between 2 web pages
	Arguments - none
	Date created - 29-04-2013 */
	public void sync() throws Exception{
		driver.sync();
 	}
 	
 	
	
	
	/* Author - Payal Garg
	Function - This function closes all the opened browsers
	Arguments - none
	Date created - 29-04-2013 */
	public void closeDriver(){ 
			driver.closeOpenedDriver();
		
	}
	
	
	
	
	
	/* Author - Payal Garg
	Function - This function search for the product
	Arguments - element(search textbox) xpath, product to be searched for, search button xpath
	Date created - 29-04-2013 */
	public void searchItem(String element,String product,String searchButton) throws Exception{
		driver.search(props.getProperty(element), product, props.getProperty(searchButton));
 	}	
	
	
	
	
	
	/* Author - Payal Garg
	Function - This function search for the product
	Arguments - None
	Date created - 29-04-2013 */
	public void browseProduct() throws Exception{
		try{
			//Clicking on the next arrow to click
			for (int j=1; j<3; j++)
			 {
	 			driver.click(props.getProperty("Search_Results_Next_Page_Link_Arrow_Footer"));
				driver.sync();
			 }
	 
			//Clicking on the previous arrow to click
			int PageLink = (int) (1 + (Math.random() * (3 - 1)));
			for (int j=3; j > PageLink; j--)
	 		{
				 driver.click(props.getProperty("Search_Results_Next_Page_Link_Arrow_Header"));
				 driver.sync();
	 		}
		}catch(Exception err){
			fail("Couldn't able to browse through product pages");
		}
	}

	
	
	
	/* Author - Payal Garg
	Function - This function view product
	Date created - 29-04-2013 */
	public void viewProduct() throws Exception {
		try{
					String parent = null;
					do{
						parent = getAttribute(props.getProperty("view_product_detail_button"));
			 			if(!parent.equals("sli_list_result not-available")){
							driver.click(props.getProperty("view_product_detail_button"));
			 				driver.sync();
						 }			
			 			} while (parent.equals("sli_list_result not-available"));	
			 
			}catch(Exception err){
				fail("cannot view the product page");
			}
		}
	
	
	
	
	/* Author - Payal Garg
	Function - This function add product
	Date created - 29-04-2013 */
 	public void addCart() throws Exception {
 		try{
			driver.click(props.getProperty("add_to_cart_button"));
		}catch(Exception err){
			fail("Product couldn't be added to cart");
		}
	 }
	
 	
 	
 	
	/* Author - Payal Garg
	Function - checkout functionality
	Date created - 29-04-2013 */
 	public void checkOut() throws Exception {
		try{
			String zip = props.getProperty("checkout_zipcode");		
			driver.type(props.getProperty("Checkout_Zip_Code"), zip);
			driver.sync();
			wd.findElement(By.xpath(props.getProperty("Checkout_Zip_Code"))).sendKeys(" ");
			List<WebElement> myele = wd.findElements(By.xpath(props.getProperty("AJAX_zip_code")));
			driver.ajaxElementSelect(props.getProperty("Checkout_Zip_Code"),props.getProperty("AJAX_zip_code"), myele, props.getProperty("list_value") );  /// still working $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
			driver.click(props.getProperty("check_out_button"));
			driver.sync();
			driver.linkClick(props.getProperty("Shipping_clear_form"));
		}catch(Exception err){
		 fail("Couldn't get to checkout page");
 
		}
 	}

}